const TOKEN_KEY = 'token_key'
const LOGIN_USER_KEY = 'login_user_key'
export const EDITABLE_TABS = 'editable_tabs'
const EDITABLE_TABS_VALUE= 'editable_tabs_value'
const MENU_SELECTED_INDEX= 'menu_selected_index'

export const setToken = (token) => {
  localStorage.setItem(TOKEN_KEY, JSON.stringify(token))
}

export const getToken = () => {
  return JSON.parse(localStorage.getItem(TOKEN_KEY))
}

export const removeToken = () => {
  localStorage.removeItem(TOKEN_KEY)
}

export const setLoginUser = (user) => {
  localStorage.setItem(LOGIN_USER_KEY, JSON.stringify(user))
}

export const getLoginUser = () => {
  return JSON.parse(localStorage.getItem(LOGIN_USER_KEY))
}

export const removeLoginUser = () => {
  localStorage.removeItem(LOGIN_USER_KEY)
}

export const setEditableTabs = (tab) => {
  if (tab) {
    let arrs = getEditableTabs()
    let flag = false
    arrs.forEach(item=>{
      if(item.title === tab.title){
        flag = true
        localStorage.setItem(EDITABLE_TABS_VALUE,JSON.stringify(item.name+''))
      }
    })

    if(!flag){
      arrs.push(tab)
      localStorage.setItem(EDITABLE_TABS,JSON.stringify(arrs))
      localStorage.setItem(EDITABLE_TABS_VALUE,JSON.stringify(tab.name+''))
    }
  } else {
    const arrs = [{
      title: '首页',
      name: "1",
      url: '/index'
    }]
    localStorage.setItem(EDITABLE_TABS,JSON.stringify(arrs))
    localStorage.setItem(EDITABLE_TABS_VALUE,JSON.stringify("1"))
  }
}

export const getEditableTabs = () => {
  return JSON.parse(localStorage.getItem(EDITABLE_TABS))
}

export const getEditableTabsValue = () => {
  return JSON.parse(localStorage.getItem(EDITABLE_TABS_VALUE))
}

export const getMenuSelectedIndex = () => {
  return JSON.parse(localStorage.getItem(MENU_SELECTED_INDEX))
}

export const setMenuSelectedIndex = (index) => {
  localStorage.setItem(MENU_SELECTED_INDEX,JSON.stringify(index))
}
